<template>
<div class="vue-box sbot sbot-fluid">
  <!-- 参数栏 -->
  <div class="c-panel">
    <el-form :inline="true">
      <!-- 表单 -->
      <sa-item v-model="m.name" type="text" name="资源名" br />
      <sa-item v-model="m.segmentName" type="text" name="简称" br />
      <sa-item v-model="m.code" type="text" name="识别码" br />
      <sa-item v-model="m.category" type="enum" jtype="4" :jv="dims.RESOURCE_CATEGORY" name="资源类别" br />
      <sa-item name="资源类型" br>
        <template>
          <el-select v-model="m.type" placeholder="资源类型" clearable>
            <el-option v-for="(k, v) in options" :key="v" :label="k" :value="v">
            </el-option>
          </el-select>
        </template>
      </sa-item>
      <sa-item name="所属标签" br>
        <template>
          <el-select v-model="selectTags" placeholder="所属标签" clearable multiple>
            <el-option v-for="(k, v) in dims.TAGS" :label="k" :value="v">
            </el-option>
          </el-select>
        </template>
      </sa-item>
      <sa-item v-model="m.url" type="text" name="资源URL" br />
      <sa-item v-model="m.img" type="img-list" name="资源图标" br />
      <sa-item v-model="m.thumbnail" type="img-list" name="缩略图" br />
      <sa-item v-model="m.note" type="textarea" name="资源描述" br />
    </el-form>
  </div>
</div>
</template>

<script>
export default {
  name: 'check-rsr',
  props: {
    param: Object
  },
  data() {
    return {
      // 超级对象
      m: this.createModel(),
      dims: sa.$sys.getCurd(),
      selectTags: [],
      options: []
    }
  },
  watch: {
    'm.category': {
      handler(n, o) {
        if (n && n !== o) {
          this.getOptions(n)
        }
      },
      immediate: true
    }
  },
  created: function () {
    if (this.param.item.tags) {
      this.selectTags = JSON.parse(this.param.item.tags)
    }
  },
  methods: {
    createModel() {
      return {
        id: this.param.item.id,
        name: this.param.item.name,
        segmentName: this.param.item.segmentName,
        code: this.param.item.code,
        category: this.param.item.category,
        type: this.param.item.type,
        tags: this.param.item.tags,
        url: this.param.item.url,
        img: this.param.item.img,
        thumbnail: this.param.item.thumbnail,
        status: this.param.item.status,
        note: this.param.item.note,
        resourceId: this.param.item.resourceId,
      }
    },
    // 变更
    ok: function () {
      // 表单校验
      let m = this.m;
      sa.checkNull(m.name, '请输入参数名');
      sa.checkNull(m.code, '请输入编码码');
      sa.checkNull(m.type, '请确认参数类型');

      // 将标准转换未字符串
      if (this.selectTags) {
        m.tags = JSON.stringify(this.selectTags)
      }

      // 资源入驻
      sa.ajax('/rsr/apply/check-in', m, function (res) {
        if (res.code == 200) {
          sa.alert(res.msg, function () {
            this.m = this.createModel();
            sa.closeModel();
            sa.f5()
          }.bind(this));
        }
      }.bind(this), {
        type: 'post'
      });
    },
    // 类型
    getOptions(param) {
      sa.ajax('/category/options', {
        id: param
      }, function (res) {
        let obj = {}
        if (res.code === 200) {
          res.data.forEach((item) => {
            obj[item.id] = item.name
          });
        }
        this.options = obj
      }.bind(this), {
        type: "get"
      })
    },
  }
}
</script>

<style scoped></style>
